home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / TODOLIST.ARJ / TODOLIST.H < prev    next >
C/C++ Source or Header  |  1991-11-11  |  11KB  |  392 lines

  1. #if !defined( __TODOLIST_H )
  2. #define __TODOLIST_H
  3.  
  4. //---------------------------------------------------------------------
  5. //
  6. //  TODOLIST.H
  7. //
  8. //      Copyright (c) 1991 by Borland International
  9. //      All Rights Reserved.
  10. //
  11. //  defines the following classes, used in implementing the Todo list:
  12. //
  13. //      TdDate      - extends the Date class from the class
  14. //                    library by providing a constructor that
  15. //                    converts a text string into a date.
  16. //
  17. //      TodoEntry   - holds the data for an entry in the Todo list.
  18. //
  19. //      TodoList    - container for holding Todo entries
  20. //
  21. //      ListBox     - wrapper around the Windows listbox, providing
  22. //                    an interface that fits with the Todo list.
  23. //
  24. //      TodoWindow  - the main window for this application.  There's
  25. //                    nothing displayed in the window except for the
  26. //                    list box.
  27. //
  28. //---------------------------------------------------------------------
  29.  
  30. #if !defined( __WINDOWS_H )
  31. #include <Windows.h>
  32. #endif  // __WINDOWS_H
  33.  
  34. #if !defined( __IOSTREAM_H )
  35. #include <iostream.h>
  36. #endif  // __IOSTREAM_H
  37.  
  38. #if !defined( __SORTABLE_H )
  39. #include "sortable.h"
  40. #endif  // __SORTABLE_H
  41.  
  42. #if !defined( __SORTARRY_H )
  43. #include "sortarry.h"
  44. #endif  // __SORTARRY_H
  45.  
  46. #if !defined( __LDATE_H )
  47. #include "ldate.h"
  48. #endif  // __LDATE_H
  49.  
  50. #if !defined( __TODOWIN_H )
  51. #include "TodoWin.h"
  52. #endif  // __TODOWIN_H
  53.  
  54. #define TodoEntryClass  __firstUserClass
  55.  
  56. //---------------------------------------------------------------------
  57. //
  58. //  class TdDate
  59. //
  60. //      extends the Date class from the class library by providing a
  61. //      constructor that converts a text string into a date.
  62. //
  63. //---------------------------------------------------------------------
  64.  
  65. class TdDate : public Date
  66. {
  67. public:
  68.  
  69.     TdDate();
  70.     TdDate( const Date& );
  71.     TdDate( const char * );
  72. };
  73.  
  74. //---------------------------------------------------------------------
  75. //
  76. //  class TodoEntry
  77. //
  78. //      holds the data for an entry in the Todo list.
  79. //
  80. //---------------------------------------------------------------------
  81.  
  82. class TodoEntry : public Sortable
  83. {
  84.     friend class EditBox;
  85.  
  86. public:
  87.  
  88.     TodoEntry();                // constructor.
  89.  
  90.     BOOL modified();            // indicates whether the entry has
  91.                                 // been modified.  Used in determining
  92.                                 // whether the list should be saved.
  93.  
  94.     virtual int             isEqual( const Object& ) const;
  95.     virtual int             isLessThan( const Object& ) const;
  96.  
  97.     virtual classType       isA() const;
  98.     virtual char            *nameOf() const;
  99.     virtual hashValueType   hashValue() const;
  100.  
  101.     friend istream& operator >> ( istream&, TodoEntry& );
  102.                                 // reads a TodoEntry from a stream.  The
  103.                                 // format used must match the format used
  104.                                 // by operator <<.
  105.  
  106.     friend ostream& operator << ( ostream&, TodoEntry& );
  107.                                 // writes a TodoEntry to a stream.  The
  108.                                 // format used must match the format used
  109.                                 // by operator >>.
  110.  
  111. protected:
  112.  
  113.     virtual void            printOn( ostream& ) const;
  114.                                 // this is the function called by
  115.                                 // operator << when applied to an Object.
  116.                                 // The disambiguation rules of C++ determine
  117.                                 // whether this function or the operator <<
  118.                                 // defined for this class will be called.
  119.                 // For example:
  120.                                 //
  121.                                 // TodoEntry td;
  122.                                 // cout << td;  // calls the friend function
  123.                                 //              // function defined above
  124.                                 // cout << (Object&)td;
  125.                                 //              // calls the operator << for
  126.                                 //              // Object, which calls
  127.                                 //              // printOn().
  128.  
  129. private:
  130.  
  131.     BOOL dirty;                 // indicates whether this entry has
  132.                                 // been modified.
  133.  
  134.     TdDate dateCreated;
  135.     TdDate dateDue;
  136.     char *text;                 // the note associated with this entry
  137.     int priority;
  138.  
  139. };
  140.  
  141. //---------------------------------------------------------------------
  142. //
  143. //  class TodoList
  144. //
  145. //      container for holding Todo entries.  Currently implemented as
  146. //      a SortedArray, so we don't have to explicitly sort the entries
  147. //      when a new one is added.  The sorting is done according to the
  148. //      operator < () defined for a TodoEntry, which sorts according
  149. //      to the due date.
  150. //
  151. //---------------------------------------------------------------------
  152.  
  153. class TodoList : public SortedArray
  154. {
  155.  
  156. public:
  157.  
  158.     TodoList();
  159.     ~TodoList();
  160.  
  161.     virtual void add( Object& );
  162.                 // adds an entry to the Todo list.
  163.  
  164.     virtual void detach( Object&, DeleteType = DefDelete );
  165.                 // removes an entry from the Todo list.
  166.  
  167.     int indexOf( const TodoEntry& );
  168.                                 // returns the index of the specified entry.
  169.  
  170.     BOOL modified();            // indicates whether the list has
  171.                                 // been modified by adding or deleting an
  172.                                 // entry.  Used in determining
  173.                                 // whether the list should be saved.
  174.  
  175.     void clear();               // removes all entries from the list.
  176.  
  177.     friend istream& operator >> ( istream&, TodoList& );
  178.                                 // reads a TodoList from a stream.  The
  179.                                 // format used must match the format used
  180.                                 // by operator <<.
  181.  
  182.     friend ostream& operator << ( ostream&, TodoList& );
  183.                                 // writes a TodoList to a stream.  The
  184.                                 // format used must match the format used
  185.                                 // by operator >>.
  186.  
  187. private:
  188.  
  189.     BOOL dirty;                 // indicates whether this list has been
  190.                                 // modified.
  191.  
  192. };
  193.  
  194. //---------------------------------------------------------------------
  195. //
  196. //  class ListBox
  197. //
  198. //      wrapper around the Windows listbox, providing an interface
  199. //      that fits with the Todo list.  This is used to display the
  200. //      Todo list in a window.
  201. //
  202. //---------------------------------------------------------------------
  203.  
  204. class ListBox
  205. {
  206.  
  207. public:
  208.  
  209.     ListBox();
  210.  
  211.     const ListBox& operator = ( const TodoList& );
  212.                                 // copies the entries in the TodoList
  213.                                 // into the list box.
  214.  
  215.     void focus();               // sets focus to the list box.
  216.     void move( const RECT& );   // moves and resizes the list box.
  217.     int current();              // returns the index of the current
  218.                                 // selection.
  219.     void remove( int );         // removes the specified entry from
  220.                 // the list box.
  221.     void insert( int, const TodoEntry& );
  222.                 // adds an entry to the list box.
  223.     void replace( int, const TodoEntry& );
  224.                                 // replaces an entry in the list box
  225.                                 // with another entry.
  226.     void select( int );         // selects the specified entry.
  227.     void clear();               // removes all entries.
  228.  
  229.     void create( HWND, HWND, const RECT& );
  230.                                 // builds the list box.  This can't be
  231.                                 // done in the constructor because we
  232.                                 // don't have enough information at the
  233.                                 // time of construction.
  234.  
  235. private:
  236.  
  237.     HWND hListBox;              // handle of the list box.
  238.  
  239. };
  240.  
  241. //---------------------------------------------------------------------
  242. //
  243. //  class TodoWindow
  244. //
  245. //      the main window for this application.  There's nothing displayed
  246. //      in the window except for the list box.
  247. //---------------------------------------------------------------------
  248.  
  249. class TodoWindow : public Window
  250. {
  251. public:
  252.  
  253.     TodoWindow();
  254.     ~TodoWindow();
  255.  
  256. protected:
  257.  
  258.     virtual LONG dispatch( WORD, WORD, LONG );
  259.  
  260.     virtual BOOL registerClass();
  261.     virtual BOOL createWindow();
  262.  
  263. private:
  264.  
  265.     ListBox listBox;            // the list box used by this window.
  266.     TodoList tdl;               // the Todo list being displayed in this
  267.                                 // window.  There's a lot of parallelism
  268.                                 // between the operations of these two
  269.                                 // objects, and it might be worthwhile
  270.                                 // to add a class derived from both
  271.                                 // ListBox and TodoList for use here.
  272.  
  273.     char *fileName;             // path to the file currently being
  274.                                 // used.  0 if there is no file.
  275.  
  276.     void newList();
  277.     void openFile();
  278.     void saveFile();
  279.     void saveFileAs();
  280.     void editBox();
  281.     void newEntry();
  282.     void delEntry();
  283.     void aboutBox();
  284.  
  285.     void fileBox();
  286.     BOOL saveBox();
  287.  
  288.     BOOL getFileName( const char *, BOOL );
  289.  
  290.     void moveListBox();
  291.  
  292.     void readFile();
  293.     void writeFile();
  294.     void checkSave();
  295.  
  296.     BOOL processCommand( WORD, LONG );
  297.  
  298. };
  299.  
  300. //---------------------------------------------------------------------
  301. //
  302. //  inline functions.
  303. //
  304. //---------------------------------------------------------------------
  305.  
  306. inline int min( int a, int b ) { return (a<b) ? a : b; }
  307.  
  308. inline TdDate::TdDate() : Date()
  309. {
  310. }
  311.  
  312. inline TdDate::TdDate( const Date& d ) : Date( d )
  313. {
  314. }
  315.  
  316. inline TodoEntry::TodoEntry() : dirty( FALSE ), priority( 1 ), text( 0 )
  317. {
  318. }
  319.  
  320. inline BOOL TodoEntry::modified()
  321. {
  322.     return dirty;
  323. }
  324.  
  325. inline TodoList::TodoList() : SortedArray( 20 )
  326. {
  327. }
  328.  
  329. inline TodoList::~TodoList()
  330. {
  331. }
  332.  
  333. inline ListBox::ListBox() : hListBox( 0 )
  334. {
  335. }
  336.  
  337. inline void ListBox::focus()
  338. {
  339.     if( IsWindow( hListBox ) )
  340.         SetFocus( hListBox );
  341. }
  342.  
  343. inline void ListBox::move( const RECT& wrect )
  344. {
  345.     MoveWindow( hListBox,
  346.                 wrect.left,
  347.                 wrect.top,
  348.                 wrect.right - wrect.left,
  349.                 wrect.bottom - wrect.top,
  350.                 TRUE
  351.               );
  352. }
  353.  
  354. inline int ListBox::current()
  355. {
  356.     return (int)SendMessage( hListBox, LB_GETCURSEL, 0, 0 );
  357. }
  358.  
  359. inline void ListBox::remove( int i )
  360. {
  361.     SendMessage( hListBox, LB_DELETESTRING, i, 0 );
  362.     select( i );
  363. }
  364.  
  365. inline void ListBox::replace( int i, const TodoEntry& tde )
  366. {
  367.     remove( i );
  368.     insert( i, tde );
  369. }
  370.  
  371. inline void ListBox::select( int i )
  372. {
  373.     i = min( i, (int)SendMessage( hListBox, LB_GETCOUNT, 0, 0 ) - 1 );
  374.     SendMessage( hListBox, LB_SETCURSEL, i, 0 );
  375. }
  376.  
  377. inline void ListBox::clear()
  378. {
  379.     SendMessage( hListBox, LB_RESETCONTENT, 0, 0 );
  380. }
  381.  
  382. inline TodoWindow::TodoWindow() : fileName( 0 )
  383. {
  384. }
  385.  
  386. inline TodoWindow::~TodoWindow()
  387. {
  388. }
  389.  
  390. #endif  // __TODOLIST_H
  391.  
  392.